<?php
namespace Home\Model;
use  Think\Model;
class YygameModel extends Model{

    protected $autoCheckFields =false;
    //随机获取奖池有奖的奖项，$xh 是否循环充奖
    function chose_prizes($xh=1)
    {
        //200人次还剩人数
        $sum_num=M("yyprizes")->where('is_lock=0')->sum('surplus');
       
         //每次抽奖的最大人数
        $max_num=M("yyprizes")->where('is_lock=0')->sum('num');
        //奖项数量
        $max_prozes=M("yyprizes")->where('is_lock=0')->count();

        //抽完-充奖
        if($xh==1&&$sum_num<1)
        {
            $sql="update ".C("DB_PREFIX")."yyprizes  set surplus=surplus+num where is_lock=0 ";

            $status=M()->execute($sql);
            //判断是否指定用户--减去奖项数量
            $u_prize_list=M("yyusertoprize")->where("status=0 ")->order("u_order desc,ctime desc")->select();
            if($u_prize_list)
            {
                foreach ($u_prize_list as $key => $value) {
                $sql="update ".C("DB_PREFIX")."yyprizes  set surplus=surplus-1 where is_lock=0 and prizes_id='".$value['prizes_id']."'";
                $status=M()->execute($sql);
                $max_prozes--;
              }            
            }


               

            
            if($status)
            $sum_num=$max_num;
        }
        
       
      
        //开始抽奖
        if($sum_num>0)
        { 

            //指定特殊用户
             $code=$_SESSION['code'];
             $u_level=0;
             $result=array();
             $u_prize_id=0;
             if($code)
             {

                //优先级--时间排序获取
                $u_prize_info=M("yyusertoprize")->where("code='$code' and status=0 ")->order("u_order desc,ctime desc")->find();
                
                if($u_prize_info)
                {
                   $prizes_id=$u_prize_info['prizes_id'];
                   $prize_info=M("yyprizes")->where("is_lock=0 and prizes_id='$prizes_id'")->find();
                    if($prize_info)//奖池还有奖
                    {
                       $level=$prize_info['level'];
                       $result['prizes_name']=$prize_info['prizes_name'];
                       $result['level']= $level;
                       $result['prizes_id']=$prize_info['prizes_id'];  

                       $u_prize_id=$u_prize_info['u_prize_id'];

                        $update_data['used_time']=time();
                        $update_data['status']=1;
                        $status=M("yyusertoprize")->where("u_prize_id='$u_prize_id'")->save($update_data);


                    }

                }
                
             }

             if(empty($result))
             {

                 //按人数计算概率
                 $list=M("yyprizes")->where('is_lock=0 and surplus>0')->order("prizes_id desc")->select();//有奖

                 //处理数据--奖项级别是key
                 $list_prizes=array();
                 foreach ($list as $key => $value) {
                     $list_prizes[$value['level']]['prizes_name']=$value['prizes_name'];
                     $list_prizes[$value['level']]['num']=$value['num'];
                     $list_prizes[$value['level']]['prizes_id']=$value['prizes_id'];
                 }

                 $rand=(100-rand(1,100))*$max_num/100;
                 //选奖
                 $level=0;
                 $level_str=0;

                 $l_keys=array_keys($list_prizes);                    
                 foreach ($list_prizes as $key => $value) {
                     //特殊判断
                     //余10才能抽1级奖
                     if($key==$l_keys[count($l_keys)-1])
                     {
                        if($sum_num>10)
                        continue;
                     }
                    
                     $r_num=$max_num-$value['num']; 
                     if($r_num>=$rand)
                     {
                        $level=$key;
                        break;
                     }
                 }

                 if($level)
                 {
                    $result=$list_prizes[$level];
                 }
                 else //未获取到-降一级获奖
                 {
                    
                    $level=$l_keys[0];
                    $result=$list_prizes[$level];

                 } 
                 //减去奖池的奖项数量
                 $status=$this->deal_prozes($result['prizes_id']);           
                 if(!$status)
                 {
                    $result['prizes_name']="参与码异常";
                    $result['level']=$level;
                    $result['prizes_id']=0;
                 }
             
             }


            


            
  


             

        }
        else //随机数选中奖已发完
        {
            $result['prizes_name']="手气奖";
            $result['level']=0;
            $result['prizes_id']=0;
        }

        return $result;
    }
    //减去奖项数量
    function deal_prozes($prizes_id)
    {
    

    
         $status=M("yyprizes")->where("prizes_id='$prizes_id'")->setDec("surplus",1);
         $_SESSION['code']='';
         //记录获奖-奖项
         $_SESSION['prizes_id']=$prizes_id;//标记已摇奖    
         

        return $status;
       
    }



    //添加获奖用户
    function add_users($data)
    {
       $yyprizes_log=M("yyprizes_log");
       $yyprizes_log->create($data);
       
       $yylog_id=$yyprizes_log->add($data);
        // print_r($yyprizes_log->_sql());exit();
       if($yylog_id)
       {
        $_SESSION['yylog_id']=$yylog_id;//标记已提交信息
        $_SESSION['prizes_id']='';
        $_SESSION['code_id']='';

       }

       
       return $yylog_id;
    }
}
?>
